﻿using CSP.Bll;
using CSP.Model.CSP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CSP.Web.MyHome.ServiceManage
{
    public partial class Content : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RequestServiceType = "52";

                BindServiceTypeUI();
                BindServiceHouse();
                BindServiceCar();
                BindServiceTrust();
            }
        }
        protected StringBuilder GetServiceChildUrl(string childId)
        {
            string[] childArray = childId.Split(',');
            int childLen = childArray.Length;
            int _childId = 0;
            StringBuilder sb = new StringBuilder();
            //StringBuilder childsb = new StringBuilder();
            for (int i = 0; i < childLen; i++)
            {
                _childId = Int32.Parse(childArray[i]);
                //int _childId = Int32.Parse(childId);
                List<ServiceAsset> serviceAsset = GetAssetData();
                List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == _childId).ToList();


                string selectGroup = string.Empty;
                foreach (ServiceAsset item in serviceData)
                {
                    if (item.Level == 999)
                    {
                        #region
                        int startIndex = 1;
                        List<ServiceAsset> serviceSubProp = GetAssetData();
                        List<ServiceAsset> __serviceSubProp = serviceSubProp.Where(s => s.Parentid == item.Id).ToList();
                        
                        foreach (ServiceAsset _subprop in __serviceSubProp)
                        {
                            sb.Append("<br />");
                            //sb.Append("<option value='" + startIndex + "'>" + _subprop.Name + "</option>");
                            sb.Append("<label>" + _subprop.Name + ":</label>");

                            List<ServiceAsset> serviceThirdProp = GetAssetData();
                            List<ServiceAsset> _serviceThirdProp = serviceSubProp.Where(s => s.Parentid == _subprop.Id).ToList();
                            if (_serviceThirdProp.Count > 0)
                            {
                                foreach (ServiceAsset __ThirdProp in _serviceThirdProp)
                                {
                                    sb.Append("<input id='child" + __ThirdProp.Id + "' type=\"radio\" name='radio" + __ThirdProp.Parentid + "'  value='" + __ThirdProp.Id + "' /><label for='child" + __ThirdProp.Id + "'>" + __ThirdProp.Name + "</label>");
                                }
                                startIndex++;
                            }
                        }

                        #endregion
                    }
                    else if (item.Name == "OTHER")
                    {
                        string __name = "limit" + item.Id.ToString();
                        sb.Append("<div class=\"ui-form-text\"><div class=\"text\"><input name='" + __name + "' type='text' value=\"\" /><span class=\"tail\">元</span></div></div>");
                    }
                    else
                    {
                        sb.Append("<input id='child" + item.Id + "' type=\"radio\" name='radio"+item.Parentid+"' value='" + item.Id + "' /><label for='child" + item.Id + "'>" + item.Name + "</label>");
                    }
                }
            }
            return sb;
        }
        private void BindServiceTypeUI()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset house = serviceAsset.FirstOrDefault(s => s.Parentid == 9);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == house.Parentid).ToList();
            this.R_Type.DataSource = serviceData;
            this.R_Type.DataBind();


            if (!string.IsNullOrWhiteSpace(_requestServiceType))
            {
                List<ServiceAsset> servicetypes = serviceAsset.Where(s => s.Parentid == Int32.Parse(_requestServiceType)).ToList();
                this.R_ServiceType.DataSource = servicetypes;
                this.R_ServiceType.DataBind();
                
            }
        }
 
        private string _requestServiceType = string.Empty;

        private string _requestServiceTypeSub = string.Empty;
        protected string RequestServiceType
        {
            get
            {
                return _requestServiceType;
            }
            set
            {
                _requestServiceType = value;
            }
        }

        protected string RequestServiceTypeSub
        {
            get
            {
                return _requestServiceTypeSub;
            }
        }

        public void BindServiceHouse()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset house = serviceAsset.FirstOrDefault(s => s.Parentid == 1);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == house.Parentid).ToList();
            //this.R_House.DataTextField = "name";
            //this.R_House.DataValueField = "id";
            this.R_House.DataSource = serviceData;
            this.R_House.DataBind();

        }
        public void BindServiceCar()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset car = serviceAsset.FirstOrDefault(s => s.Parentid == 2);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == car.Parentid).ToList();
            this.R_Car.DataSource = serviceData;
            this.R_Car.DataBind();

        }

        public void BindServiceTrust()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset trust = serviceAsset.FirstOrDefault(s => s.Parentid == 3);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == trust.Parentid).ToList();
            this.R_Trust.DataSource = serviceData;
            this.R_Trust.DataBind();

        }
        private List<CSP.Model.CSP.ServiceAsset> GetAssetData()
        {
            //List<AgeGroup> result = new List<AgeGroup>();
            //return result;

            List<CSP.Model.CSP.ServiceAsset> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceAssetBll())
            {
                result = serviceTypeBll.FindAll().ToList();
            }
            return result;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write(RHOUSE.Value + "rrrrrrrr" + RCAR.Value + "rrrrrrrr" + RTRUST.Value + "rrrrrrrr" + SERVICEINFO.Value + "rrrrrrrr");
        }

  

    }
}